package com.bwie.service.impl;

import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bwie.poi.StringUtils;
import com.bwie.utils.JwtsUtils;
import com.bwie.utils.R;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.Type;
import com.bwie.mapper.TypeMapper;
import com.bwie.service.TypeService;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
* @author :Beyond🎸🎸
* @date : 2025-10-19 20:47:56
* @description
* ${description}
**/
@Service
public class TypeServiceImpl extends ServiceImpl<TypeMapper, Type> implements TypeService{



    @Autowired
    private FastFileStorageClient fileStorageClient;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private TypeMapper typeMapper;
    @Override
    public R uploadFile(MultipartFile file) {

        try {
            String extName = FileNameUtil.extName(file.getOriginalFilename());
            StorePath storePath = fileStorageClient.uploadFile(file.getInputStream(), file.getSize(), extName, null);
            String path = "http://192.168.80.192:8888/"+storePath.getFullPath();
            return R.OK(path);
        } catch (IOException e) {
            e.printStackTrace();
            return R.ERROR(500,"上传失败");
        }

    }

    @Override
    public R saveType(Type type) {

        String token = request.getHeader("token");

        Integer userIdByToken = JwtsUtils.getUserIdByToken(token);
        type.setCreateTime(new Date());
        type.setDeleteFlag(0);
        type.setCreateBy(userIdByToken);
        if(type.getTypePid()==null){
            type.setTypePid(0);
        }
        typeMapper.insert(type);
        return R.OK();
    }

    @Override
    public R selectTypeList(Type type) {

        LambdaQueryWrapper<Type> wrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotEmpty(type.getTypeName())){
            wrapper.like(Type::getTypeName,type.getTypeName());
        }
        if(StringUtils.isNotEmpty(type.getTypeName())){
            wrapper.eq(Type::getTypeStatus,type.getTypeStatus());
        }
        wrapper.eq(Type::getDeleteFlag,0);

        List<Type> typeList = typeMapper.selectList(wrapper);

        List<Tree<Integer>> build = TreeUtil.build(typeList, 0, (tree, treeNode) -> {
            treeNode.setId(tree.getTypeId());
            treeNode.setParentId(tree.getTypePid());
            treeNode.setName(tree.getTypeName());
            treeNode.putExtra("image",tree.getTypeImage());
            treeNode.putExtra("top",tree.getTypeTop());
            treeNode.putExtra("status",tree.getTypeStatus());
            treeNode.putExtra("value",tree.getTypeId().toString());
            treeNode.putExtra("label",tree.getTypeName());
        });

        return R.OK(build);
    }
}
